package com.njym.java.security.app.rest;

import com.njym.java.security.app.biz.FamilyUserBiz;
import com.njym.java.security.app.biz.TaskClassifyBiz;
import com.njym.java.security.app.biz.TaskListBiz;
import com.njym.java.security.app.biz.TaskUserRelevanceBiz;
import com.njym.java.security.app.entity.*;
import com.njym.java.security.common.msg.ObjectRestResponse;
import com.njym.java.security.common.rest.BaseController;
import com.njym.java.security.common.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.*;
import java.util.stream.Collectors;

@Controller
@RequestMapping("taskUserRelevance")
public class TaskUserRelevanceController extends BaseController<TaskUserRelevanceBiz, TaskUserRelevance> {

	@Autowired
	private TaskUserRelevanceBiz taskUserRelevanceBiz;

	@Autowired
	private TaskListBiz taskListBiz;

	@Autowired
	private TaskClassifyBiz taskClassifyBiz;

	@Resource
	private FamilyUserBiz familyUserBiz;

	/**
	 * 查询成员
	 *
	 * @param fid
	 * @return
	 */
	@RequestMapping(value = "/taskList", method = RequestMethod.GET)
	@ResponseBody
	public Object taskList(String fid) {
		if (!fid.equals("undefined")) {
			List<TaskClassifyVo> taskClassifyVo = taskClassifyBiz.getLIst();
			List<TaskUserRelevanceVo> list = taskUserRelevanceBiz.getTaskByfid(Integer.parseInt(fid));
			for (TaskClassifyVo taskc : taskClassifyVo) {
				ArrayList<TaskUserRelevanceVo> lists = new ArrayList<TaskUserRelevanceVo>();
				for (TaskUserRelevanceVo taskUserRelevanceVo : list) {
					if (taskUserRelevanceVo.getTask_classify_id().equals(taskc.getId())) {
						lists.add(taskUserRelevanceVo);
					}
				}
				taskc.setTaskList(lists);
			}
			return ResponseUtil.ok(taskClassifyVo);
		} else {
			return ResponseUtil.fail(40301, "错误！");
		}
	}


	/**
	 * 查询成员
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/taskListByid", method = RequestMethod.GET)
	@ResponseBody
	public Object taskListByid(String id,String fid) {
		if (!id.equals("undefined") && !fid.equals("undefined")) {
			Example example = new Example(TaskUserRelevance.class);
			example.createCriteria().andEqualTo("taskId", id).andEqualTo("deleted", 0).andEqualTo("modelFlag", 1);
			List<TaskUserRelevance> listMod = baseBiz.selectByExample(example);
		    String mod = "";
			for(TaskUserRelevance taskUserRelevance : listMod ){
			 FamilyUser user = 	familyUserBiz.selectById(taskUserRelevance.getFamilyId());
				mod = mod+user.getName()+"*"+user.getId()+",";
			}
			TaskUserRelevanceVo taskUserRelevanceVo = taskUserRelevanceBiz.getTaskByid(Integer.parseInt(id),Integer.parseInt(fid));
           if(mod.length()>1){
               taskUserRelevanceVo.setModel_family(mod.substring(0,mod.length()-1));
           }
			return ResponseUtil.ok(taskUserRelevanceVo);
		}else{
			return ResponseUtil.fail(-1,"错误");
		}

	}




	@RequestMapping(value = "/getAllTask", method = RequestMethod.GET)
	@ResponseBody
	public Object getAllTask(@NotBlank String pid) {
		if (!pid.equals("undefined")) {
			//获取所有任务
			Example example = new Example(TaskUserRelevance.class);
			example.createCriteria().andEqualTo("familyId", Integer.parseInt(pid)).andEqualTo("deleted", 0);
			List<TaskUserRelevance> list = baseBiz.selectByExample(example);
			//未接受
			List<TaskUserRelevance> unAcceptList = list.stream().filter(e -> e.getStatus() == 0).collect(Collectors.toList());
			//正在完成
			List<TaskUserRelevance> doingList = list.stream().filter(e -> e.getStatus() == 1).collect(Collectors.toList());
			//已完成
			List<TaskUserRelevance> finishedList = list.stream().filter(e -> e.getStatus() == 2).collect(Collectors.toList());
			Map<String, Object> map = new HashMap<>(4);
			map.put("unaccept", getList(unAcceptList));
			map.put("doing", getList(doingList));
			map.put("finished", getList(finishedList));
			return new ObjectRestResponse<>().rel(true).data(map);
		} else {
			return ResponseUtil.fail(40301, "错误！");
		}

	}

	@RequestMapping(value = "/saveTask", method = RequestMethod.GET)
	@ResponseBody
	public Object saveTask(@NotBlank String userId,@NotBlank String taskId) {
		if (!userId.equals("undefined") && !taskId.equals("undefined")) {
			FamilyUser familyUser = familyUserBiz.selectById(Integer.parseInt(userId));
			TaskUserRelevance taskUserRelevance = new TaskUserRelevance();
			taskUserRelevance.setFamilyId(familyUser.getPid());
			taskUserRelevance.setTaskId(Integer.parseInt(taskId));
			taskUserRelevance.setDeleted(0);
			TaskUserRelevance taskUserRelevance1 = baseBiz.selectOne(taskUserRelevance);
			taskUserRelevance1.setStatus(1);
			baseBiz.updateById(taskUserRelevance1);
			return new ObjectRestResponse<>().rel(true);
		} else {
			return ResponseUtil.fail(40301, "错误！");
		}

	}

	/**
	 * 修改状态
	 * @param fid
	 * @param taskId
	 * @param status
	 * @return
	 */
	@RequestMapping(value = "/updateStatus", method = RequestMethod.GET)
	@ResponseBody
	public Object updateStatus(String fid, String taskId, String status){
		if (!fid.equals("undefined") && !taskId.equals("undefined") && !status.equals("undefined")) {
			TaskUserRelevance taskUserRelevance =	taskUserRelevanceBiz.getTaskUser(Integer.parseInt(fid),Integer.parseInt(taskId));
			taskUserRelevance.setStatus(Integer.parseInt(status));
			baseBiz.updateById(taskUserRelevance);
			return ResponseUtil.ok();
		} else {
			return ResponseUtil.fail(40301, "错误！");
		}

	}

	@RequestMapping(value = "/addTaskExperience", method = RequestMethod.GET)
	@ResponseBody
	public Object addTaskExperience(String fid, String taskId, String status, String attr1, String attr2){
		if (!fid.equals("undefined") && !taskId.equals("undefined") && !status.equals("undefined")) {
			TaskUserRelevance taskUserRelevance =	taskUserRelevanceBiz.getTaskUser(Integer.parseInt(fid),Integer.parseInt(taskId));
			taskUserRelevance.setStatus(Integer.parseInt(status));
			taskUserRelevance.setAttr1(attr1);
			taskUserRelevance.setAttr2(attr2);
			taskUserRelevance.setFinishTime(new Date());
			baseBiz.updateById(taskUserRelevance);
			return ResponseUtil.ok();
		} else {
			return ResponseUtil.fail(40301, "错误！");
		}

	}

    @RequestMapping(value = "/getMod", method = RequestMethod.GET)
    @ResponseBody
    public Object getMod(String fid,String taskId){
		if (!fid.equals("undefined") && !taskId.equals("undefined") ) {
			Example example = new Example(TaskUserRelevance.class);
			example.createCriteria().andEqualTo("familyId", Integer.parseInt(fid)).andEqualTo("taskId", Integer.parseInt(taskId)).andEqualTo("deleted", 0);
			List<TaskUserRelevance> list = baseBiz.selectByExample(example);
			return ResponseUtil.ok(list.get(0));
		} else {
			return ResponseUtil.fail(40301, "错误！");
		}

    }



	private List getList(List<TaskUserRelevance> list) {
		if (list.size() == 0) {
			return null;
		}
		List<Integer> taskIds = list.stream().map(e -> e.getTaskId()).collect(Collectors.toList());
		Example example1 = new Example(TaskList.class);
		example1.createCriteria().andIn("id", taskIds).andEqualTo("deleted", 0);
		return taskListBiz.selectByExample(example1);
	}
}